$(function () {
    $("#jqGrid").jqGrid({
        url: ctx + '/sys/generator/list',
        datatype: "json",
        colModel: [			
			{ label: '表名', name: 'tableName', width: 100, key: true },
			{ label: 'Engine', name: 'engine', width: 70},
			{ label: '表备注', name: 'tableComment', width: 100 },
			{ label: '创建时间', name: 'createTime', width: 100 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50,100,200],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $("#moduleName").bind('input propertychange', function() {
        var inputVal = $("#moduleName").val();
        var inputs = $("#outDirTemplate input");
        $.each(inputs, function(index, item){
            item=$(item);
            var val = item.attr("val");
            if(typeof(val) != "undefined" && val != ''){
                var a = item.val().replace("{"+moduleName+"}","{"+inputVal+"}");
                item.val(a);
            }
        });
        moduleName = inputVal;
    });
    $("#package").bind('change', function() {
       var value = this.value;
       if(value == "zip"){
           $("#params").hide();
       }else{
           $("#params").show();
       }
    });
});
var moduleName = "";
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			tableName: null
		}
	},
	methods: {
		query: function () {
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'tableName': vm.q.tableName},
                page:1 
            }).trigger("reloadGrid");
		},
		generator: function() {
			var tableNames = getSelectedRows();
			if(tableNames == null){
				return ;
			}
			moduleName = tableNames[0];
            if(moduleName.indexOf("_") != -1){
                moduleName = tableNames[0].substring(0, tableNames[0].indexOf("_")).toLowerCase();
            }
            $("#tablePrefix").val(tableNames[0].substring(0, tableNames[0].indexOf("_")+1).toLowerCase());
            $("#tableSuffix").val(tableNames[0].substring(tableNames[0].lastIndexOf("_"), tableNames[0].length).toLowerCase());

            var inputs = $("#outDirTemplate input");
            $.each(inputs, function(index, item){
                item=$(item);
                var val = item.attr("val");
                if(typeof(val) != "undefined" && val != ''){
                    item.val(val.replace("{moduleName}","{"+moduleName+"}"));
                }
            });
            $("#moduleName").val(moduleName);

            layer.open({
                type : 1,
                title : '表名：'+tableNames[0],
                maxmin : true,
                shadeClose : false, // 点击遮罩关闭层
                area:['100%','100%'],
                content: $("#outDirTemplate"),
                btn:['确定'],
                yes: function(index, layero){
                    var tablePrefix = $("#tablePrefix").val();
                    var tableSuffix = $("#tableSuffix").val();
                    var package = $("#package").val();

                    if(package == "zip"){
                        location.href = ctx + "/sys/generator/codeZip?" +
                            "tablePrefix=" + tablePrefix +
                            "&tableSuffix=" + tableSuffix +
                            "&moduleName=" + moduleName +
                            "&rpc=" + $("#rpc").val() +
                            "&zip=true&tables=" + tableNames.join();
                        layer.closeAll();
                    }else if(package == "dir"){
                        layer.confirm('此操作会覆盖已有文件？第一次请忽略！', {
                            btn: ['确定','容我三思'] //按钮
                        }, function(){

                            var mainOutPath = $("#mainOutPath").val();
                            var serviceOutPath = $("#serviceOutPath").val().replace("{项目主目录}",mainOutPath);
                            var boOutPath = $("#boOutPath").val().replace("{项目主目录}",mainOutPath);
                            var daoOutPath = $("#daoOutPath").val().replace("{项目主目录}",mainOutPath);
                            var entityOutPath = $("#entityOutPath").val().replace("{项目主目录}",mainOutPath);
                            var dtoOutPath = $("#dtoOutPath").val().replace("{项目主目录}",mainOutPath);
                            var mapperOutPath = $("#mapperOutPath").val().replace("{项目主目录}",mainOutPath);
                            var staticOutPath = $("#staticOutPath").val().replace("{项目主目录}",mainOutPath);

                            var controllerOutPath = $("#controllerOutPath").val().replace("{项目主目录}",mainOutPath);
                            var htmlOutPath = $("#htmlOutPath").val().replace("{项目主目录}",mainOutPath);
                            var params ={
                                entityOutPath: entityOutPath.replace("{","").replace("}",""),
                                dtoOutPath: dtoOutPath.replace("{","").replace("}",""),
                                daoOutPath: daoOutPath.replace("{","").replace("}",""),
                                controllerOutPath: controllerOutPath.replace("{","").replace("}",""),
                                staticOutPath: staticOutPath.replace("{","").replace("}",""),
                                serviceOutPath: serviceOutPath.replace("{","").replace("}",""),
                                boOutPath: boOutPath.replace("{","").replace("}",""),
                                mapperOutPath: mapperOutPath.replace("{","").replace("}",""),
                                htmlOutPath: htmlOutPath.replace("{","").replace("}",""),
                                moduleName: moduleName,
                                tablePrefix: tablePrefix,
                                tableSuffix: tableSuffix,
                                zip: false,
                                rpc: $("#rpc").val(),
                                tables: tableNames.join()
                            }
                            $.ajax({
                                type: "post",
                                data: JSON.stringify(params),
                                dataType: "json",
                                url: "/sys/generator/codeDir",
                                cache:false,
                                headers : {"Content-Type" : "application/json;charset=utf-8"},
                                success: function (data) {
                                    if(data.code == 0){
                                        alert("操作成功!");
                                        layer.closeAll();
                                    }else{
                                        alert(data.msg);
                                    }
                                }
                            });
                        }, function(){

                        });
                    }
                }
            });

            // layer.confirm('代码输出方式？', {
            //     btn: ['ZIP包下载','本地项目目录'] //按钮
            // }, function(){
            //     location.href = ctx+"/sys/generator/codeZip?moduleName="+moduleName+"&isZip=true&tables=" + tableNames.join();
            //     layer.closeAll();
            // }, function(){
            //     var inputs = $("#outDirTemplate input");
            //     $.each(inputs, function(index, item){
            //         item=$(item);
            //         var val = item.attr("val");
            //         if(typeof(val) != "undefined" && val != ''){
            //             item.val(val.replace("{moduleName}","{"+moduleName+"}"));
            //         }
            //     });
            //     $("#moduleName").val(moduleName);
            //     layer.open({
            //         type : 1,
            //         title : '增加',
            //         maxmin : true,
            //         shadeClose : false, // 点击遮罩关闭层
            //         area:['100%','100%'],
            //         content: $("#outDirTemplate"),
            //         btn:['确定'],
            //         yes: function(index, layero){
            //             var mainOutPath = $("#mainOutPath").val();
            //             var serviceOutPath = $("#serviceOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var daoOutPath = $("#daoOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var entityOutPath = $("#entityOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var mapperOutPath = $("#mapperOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var staticOutPath = $("#staticOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var tablePrefix = $("#tablePrefix").val();
            //             var controllerOutPath = $("#controllerOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var htmlOutPath = $("#htmlOutPath").val().replace("{项目主目录}",mainOutPath);
            //             var params ={
            //                 entityOutPath: entityOutPath.replace("{","").replace("}",""),
            //                 daoOutPath: daoOutPath.replace("{","").replace("}",""),
            //                 controllerOutPath: controllerOutPath.replace("{","").replace("}",""),
            //                 staticOutPath: staticOutPath.replace("{","").replace("}",""),
            //                 serviceOutPath: serviceOutPath.replace("{","").replace("}",""),
            //                 mapperOutPath: mapperOutPath.replace("{","").replace("}",""),
            //                 htmlOutPath: htmlOutPath.replace("{","").replace("}",""),
            //                 moduleName: moduleName,
            //                 tablePrefix: tablePrefix,
            //                 isZip: false,
            //                 tables: tableNames.join()
            //             }
            //             $.ajax({
            //                 type: "post",
            //                 data: JSON.stringify(params),
            //                 dataType: "json",
            //                 url: "/sys/generator/codeDir",
            //                 cache:false,
            //                 headers : {"Content-Type" : "application/json;charset=utf-8"},
            //                 success: function (data) {
            //                     if(data.code == 0){
            //                         alert("操作成功!菜单初始化脚本在Dao目录，执行后可以删除");
            //                         layer.closeAll();
            //                     }else{
            //                         alert(data.msg);
            //                     }
            //                 }
            //             });
            //         }
            //     });
            // });
            //
		}
	}
});

